﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Bussines;

namespace Bussines
{
    public class clRIGHTS
    {
        private SqlConnection sc;
        private int _USR_ID = -1;

        SqlDataAdapter adapterRIGHTS;

        DataSet dsRIGHTS;

        public clRIGHTS(TBLObj iBLObj, int iUSR_ID)
        {
            TBLObj pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;
            _USR_ID = iUSR_ID;
        }

        public void SetUSR_ID(int iUSR_ID)
        {
            _USR_ID = iUSR_ID;
        }

        public DataSet GetRIGHTS()
        {
            dsRIGHTS = new DataSet();
            adapterRIGHTS = new SqlDataAdapter();

            if (_USR_ID.ToString() != "-1")
            {
                adapterRIGHTS.SelectCommand = new SqlCommand("SELECT " +
                    " RGH_ID,RGH_MEGNEVEZES,ISNULL(RGS_ID , -1) AS RGS_ID,ISNULL(RGS_RIGHT , 0) AS RGS_RIGHT, " +
                    " CASE ISNULL(RGS_RIGHT , 0) WHEN 0 THEN '' ELSE 'X' END AS RIGHTS " +
                    "FROM _RIGHT_KIND " +
                    "JOIN _USER ON 1 = 1 " +
                    "LEFT JOIN _RIGHTS ON RGH_ID = RGS_RGH_ID AND USR_ID = RGS_USR_ID " +
                    "WHERE USR_ID = " + _USR_ID.ToString() + "  " + 
                   // "WHERE (RGS_USR_ID = " + _USR_ID .ToString() + " OR RGS_USR_ID IS NULL) " +
                    "ORDER BY 1 " , sc);
            }
            else
            {
                adapterRIGHTS.SelectCommand = new SqlCommand("SELECT " +
                    " RGH_ID,RGH_MEGNEVEZES, -1 AS RGS_ID, 0 AS RGS_RIGHT, " +
                    " 'X'  AS RIGHTS " +
                    "FROM _RIGHT_KIND " +
                    "ORDER BY 1 " , sc);
            }



            
            adapterRIGHTS.Fill(dsRIGHTS, "_RIGHTS");

            return dsRIGHTS;
        }
        public bool SaveRight(int iRGH_ID, int iRIGHT, ref string oErr)
        {
            int iRGS_ID=-1;

            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd_insert = new SqlCommand();
            SqlDataAdapter Adapter = new SqlDataAdapter();
            DataSet DS = new DataSet();
            string seg = "";

            Adapter.SelectCommand =  new SqlCommand("SELECT RGS_ID FROM _RIGHTS " +
                "WHERE RGS_USR_ID = " + _USR_ID.ToString() + " " +
                "AND RGS_RGH_ID = " + iRGH_ID.ToString() , sc) ;
            Adapter.Fill(DS, "_RIGHTS");
            if (DS.Tables["_RIGHTS"].Rows.Count > 0)
                seg = DS.Tables["_RIGHTS"].Rows[0]["RGS_ID"].ToString();

            if (seg == "")
                iRGS_ID = -1;
            else
                iRGS_ID = Convert.ToInt32( seg );

            oErr = "";

            cmd.Connection = sc;
            cmd_insert.Connection = sc;
            
            cmd.CommandType = CommandType.Text;
            cmd_insert.CommandType = CommandType.Text;

            cmd.CommandText = "UPDATE _RIGHTS " +
                "SET RGS_RIGHT = @RGS_RIGHT " +
                "WHERE RGS_ID =@RGS_ID ";
            
            cmd.Parameters.Add(new SqlParameter("RGS_RIGHT", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("RGS_ID", SqlDbType.Int));

            cmd.Parameters["RGS_ID"].Value = iRGS_ID;
            cmd.Parameters["RGS_RIGHT"].Value = iRIGHT;

            cmd_insert.CommandText = "INSERT INTO _RIGHTS " +
                "(RGS_USR_ID, RGS_RGH_ID, RGS_RIGHT) " +
                "VALUES " +
                "( @RGS_USR_ID , @RGS_RGH_ID , @RGS_RIGHT ) ";

            cmd_insert.Parameters.Add(new SqlParameter("RGS_RIGHT", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("RGS_USR_ID", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("RGS_RGH_ID", SqlDbType.Int));

            cmd_insert.Parameters["RGS_USR_ID"].Value = _USR_ID ;
            cmd_insert.Parameters["RGS_RGH_ID"].Value = iRGH_ID;
            cmd_insert.Parameters["RGS_RIGHT"].Value = iRIGHT;


            
                if (iRGS_ID == -1)
                {
                    cmd_insert.ExecuteNonQuery();
                }
                else
                {
                    cmd.ExecuteNonQuery();
                }
                return true;
           
        }
    }
}
